/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Waybill;
import com.yuhanginfo.modules.sys.service.WaybillService;

/**
 * 订单详情Controller
 * @author yangs
 * @version 2017-09-19
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/waybill")
public class WaybillController extends BaseController {

	@Autowired
	private WaybillService waybillService;
	
	@ModelAttribute
	public Waybill get(@RequestParam(required=false) String id) {
		Waybill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = waybillService.get(id);
		}
		if (entity == null){
			entity = new Waybill();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:waybill:view")
	@RequestMapping(value = {"list", ""})
	public String list(Waybill waybill, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Waybill> page = waybillService.findPage(new Page<Waybill>(request, response), waybill); 
		model.addAttribute("page", page);
		return "modules/sys/waybillList";
	}

	@RequiresPermissions("sys:waybill:view")
	@RequestMapping(value = "form")
	public String form(Waybill waybill, Model model) {
		model.addAttribute("waybill", waybill);
		return "modules/sys/waybillForm";
	}

	@RequiresPermissions("sys:waybill:edit")
	@RequestMapping(value = "save")
	public String save(Waybill waybill, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, waybill)){
			return form(waybill, model);
		}
		waybillService.save(waybill);
		addMessage(redirectAttributes, "保存订单详情成功");
		return "redirect:"+Global.getAdminPath()+"/sys/waybill/?repage";
	}
	
	@RequiresPermissions("sys:waybill:edit")
	@RequestMapping(value = "delete")
	public String delete(Waybill waybill, RedirectAttributes redirectAttributes) {
		waybillService.delete(waybill);
		addMessage(redirectAttributes, "删除订单详情成功");
		return "redirect:"+Global.getAdminPath()+"/sys/waybill/?repage";
	}

}